home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d982.lha / HWGRCS / HWGdiffP1_2_6.lha / diffutils-2.6 / src / util.c < prev    next >
C/C++ Source or Header  |  1993-12-18  |  20KB  |  761 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. /* Queue up one-line messages to be printed at the end,
  23.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  24.  
  25. struct msg
  26. {
  27.   struct msg *next;
  28.   char const *format;
  29.   char const *arg1;
  30.   char const *arg2;
  31.   char const *arg3;
  32.   char const *arg4;
  33. };
  34.  
  35. /* Head of the chain of queues messages.  */
  36.  
  37. static struct msg *msg_chain;
  38.  
  39. /* Tail of the chain of queues messages.  */
  40.  
  41. static struct msg **msg_chain_end = &msg_chain;
  42.  
  43. /* Use when a system call returns non-zero status.
  44.    TEXT should normally be the file name.  */
  45.  
  46. void
  47. perror_with_name (text)
  48.      char const *text;
  49. {
  50.   int e = errno;
  51.   fprintf (stderr, "%s: ", program);
  52.   errno = e;
  53.   perror (text);
  54. }
  55.  
  56. /* Use when a system call returns non-zero status and that is fatal.  */
  57.  
  58. void
  59. pfatal_with_name (text)
  60.      char const *text;
  61. {
  62.   int e = errno;
  63.   print_message_queue ();
  64.   fprintf (stderr, "%s: ", program);
  65.   errno = e;
  66.   perror (text);
  67.   exit (2);
  68. }
  69.  
  70. /* Print an error message from the format-string FORMAT
  71.    with args ARG1 and ARG2.  */
  72.  
  73. void
  74. error (format, arg, arg1)
  75.      char const *format, *arg, *arg1;
  76. {
  77.   fprintf (stderr, "%s: ", program);
  78.   fprintf (stderr, format, arg, arg1);
  79.   fprintf (stderr, "\n");
  80. }
  81.  
  82. /* Print an error message containing the string TEXT, then exit.  */
  83.  
  84. void
  85. fatal (m)
  86.      char const *m;
  87. {
  88.   print_message_queue ();
  89.   error ("%s", m, 0);
  90.   exit (2);
  91. }
  92.  
  93. /* Like printf, except if -l in effect then save the message and print later.
  94.    This is used for things like "binary files differ" and "Only in ...".  */
  95.  
  96. void
  97. message (format, arg1, arg2)
  98.      char const *format, *arg1, *arg2;
  99. {
  100.   message5 (format, arg1, arg2, 0, 0);
  101. }
  102.  
  103. void
  104. message5 (format, arg1, arg2, arg3, arg4)
  105.      char const *format, *arg1, *arg2, *arg3, *arg4;
  106. {
  107.   if (paginate_flag)
  108.     {
  109.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  110.       new->format = format;
  111.       new->arg1 = concat (arg1, "", "");
  112.       new->arg2 = concat (arg2, "", "");
  113.       new->arg3 = arg3 ? concat (arg3, "", "") : 0;
  114.       new->arg4 = arg4 ? concat (arg4, "", "") : 0;
  115.       new->next = 0;
  116.       *msg_chain_end = new;
  117.       msg_chain_end = &new->next;
  118.     }
  119.   else
  120.     {
  121.       if (sdiff_help_sdiff)
  122.         putchar (' ');
  123.       printf (format, arg1, arg2, arg3, arg4);
  124.     }
  125. }
  126.  
  127. /* Output all the messages that were saved up by calls to `message'.  */
  128.  
  129. void
  130. print_message_queue ()
  131. {
  132.   struct msg *m;
  133.  
  134.   for (m = msg_chain; m; m = m->next)
  135.     printf (m->format, m->arg1, m->arg2, m->arg3, m->arg4);
  136. }
  137.  
  138. /* Call before outputting the results of comparing files NAME0 and NAME1
  139.    to set up OUTFILE, the stdio stream for the output to go to.
  140.  
  141.    Usually, OUTFILE is just stdout.  But when -l was specified
  142.    we fork off a `pr' and make OUTFILE a pipe to it.
  143.    `pr' then outputs to our stdout.  */
  144.  
  145. static char const *current_name0;
  146. static char const *current_name1;
  147. static int current_depth;
  148.  
  149. void
  150. setup_output (name0, name1, depth)
  151.      char const *name0, *name1;
  152.      int depth;
  153. {
  154.   current_name0 = name0;
  155.   current_name1 = name1;
  156.   current_depth = depth;
  157.   outfile = 0;
  158. }
  159.  
  160. static pid_t pr_pid;
  161.  
  162. void
  163. begin_output ()
  164. {
  165.   char *name;
  166.  
  167.   if (outfile != 0)
  168.     return;
  169.  
  170.   /* Construct the header of this piece of diff.  */
  171.   name = xmalloc (strlen (current_name0) + strlen (current_name1)
  172.                   + strlen (switch_string) + 7);
  173.   /* Posix.2 section 4.17.6.1.1 specifies this format.  But there are some
  174.      bugs in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):
  175.      it says that we must print only the last component of the pathnames,
  176.      and it requires two spaces after "diff" if there are no options.
  177.      These requirements are silly and do not match historical practice.  */
  178.   sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
  179.  
  180.   if (paginate_flag)
  181.     {
  182. #ifdef _AMIGA
  183.       FILE *popenl();
  184.       outfile = popenl(PR_FILE_NAME, "OUTPUT=\"\"", "HEADERNAME", name, NULL, "w");
  185. #else
  186.       int pipes[2];
  187.  
  188.       /* Fork a `pr' and make OUTFILE a pipe to it.  */
  189.       if (pipe (pipes) < 0)
  190.         pfatal_with_name ("pipe");
  191.  
  192.       fflush (stdout);
  193.  
  194.       pr_pid = vfork ();
  195.       if (pr_pid < 0)
  196.         pfatal_with_name ("vfork");
  197.  
  198.       if (pr_pid == 0)
  199.         {
  200.           close (pipes[1]);
  201.           if (pipes[0] != STDIN_FILENO)
  202.             {
  203.               if (dup2 (pipes[0], STDIN_FILENO) < 0)
  204.                 pfatal_with_name ("dup2");
  205.               close (pipes[0]);
  206.             }
  207.  
  208.           execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0);
  209.           pfatal_with_name (PR_FILE_NAME);
  210.         }
  211.       else
  212.         {
  213.           close (pipes[0]);
  214.           outfile = fdopen (pipes[1], "w");
  215.         }
  216. #endif
  217.     }
  218.   else
  219.     {
  220.  
  221.       /* If -l was not specified, output the diff straight to `stdout'.  */
  222.  
  223.       outfile = stdout;
  224.  
  225.       /* If handling multiple files (because scanning a directory),
  226.          print which files the following output is about.  */
  227.       if (current_depth > 0)
  228.         printf ("%s\n", name);
  229.     }
  230.  
  231.   free (name);
  232.  
  233.   /* A special header is needed at the beginning of context output.  */
  234.   switch (output_style)
  235.     {
  236.     case OUTPUT_CONTEXT:
  237.       print_context_header (files, 0);
  238.       break;
  239.  
  240.     case OUTPUT_UNIFIED:
  241.       print_context_header (files, 1);
  242.       break;
  243.  
  244.     default:
  245.       break;
  246.     }
  247. }
  248.  
  249. /* Call after the end of output of diffs for one file.
  250.    Close OUTFILE and get rid of the `pr' subfork.  */
  251.  
  252. void
  253. finish_output ()
  254. {
  255.   if (outfile != 0 && outfile != stdout)
  256.     {
  257.       int wstatus;
  258. #ifdef _AMIGA
  259.       wstatus = pclose(outfile);
  260. #else
  261.       if (ferror (outfile))
  262.         fatal ("write error");
  263.       if (fclose (outfile) != 0)
  264.         pfatal_with_name ("write error");
  265. #if HAVE_WAITPID
  266.       if (waitpid (pr_pid, &wstatus, 0) < 0)
  267.         pfatal_with_name ("waitpid");
  268. #else
  269.       for (;;) {
  270.         pid_t w = wait (&wstatus);
  271.         if (w < 0)
  272.           pfatal_with_name ("wait");
  273.         if (w == pr_pid)
  274.           break;
  275.       }
  276. #endif
  277. #endif  /* _AMIGA */
  278.       if (! WIFEXITED (wstatus) || WEXITSTATUS (wstatus) != 0)
  279.         fatal ("subsidiary pr failed");
  280.     }
  281.  
  282.   outfile = 0;
  283. }
  284.  
  285. /* Compare two lines (typically one from each input file)
  286.    according to the command line options.
  287.    Return 1 if the lines differ, like `memcmp'.  */
  288.  
  289. int
  290. line_cmp (s1, len1, s2, len2)
  291.      char const *s1, *s2;
  292.      size_t len1, len2;
  293. {
  294.   register unsigned char const *t1, *t2;
  295.   register unsigned char end_char = line_end_char;
  296.  
  297.   /* Check first for exact identity.
  298.      If that is true, return 0 immediately.
  299.      This detects the common case of exact identity
  300.      faster than complete comparison would.  */
  301.  
  302.   if (len1 == len2 && memcmp (s1, s2, len1) == 0)
  303.     return 0;
  304.  
  305.   /* Not exactly identical, but perhaps they match anyway
  306.      when case or white space is ignored.  */
  307.  
  308.   if (ignore_case_flag | ignore_space_change_flag | ignore_all_space_flag)
  309.     {
  310.       t1 = (unsigned char const *) s1;
  311.       t2 = (unsigned char const *) s2;
  312.  
  313.       while (1)
  314.         {
  315.           register unsigned char c1 = *t1++;
  316.           register unsigned char c2 = *t2++;
  317.  
  318.           /* Ignore horizontal white space if -b or -w is specified.  */
  319.  
  320.           if (ignore_all_space_flag)
  321.             {
  322.               /* For -w, just skip past any white space.  */
  323.               while (isspace (c1) && c1 != end_char) c1 = *t1++;
  324.               while (isspace (c2) && c2 != end_char) c2 = *t2++;
  325.             }
  326.           else if (ignore_space_change_flag)
  327.             {
  328.               /* For -b, advance past any sequence of white space in line 1
  329.                  and consider it just one Space, or nothing at all
  330.                  if it is at the end of the line.  */
  331.               if (isspace (c1))
  332.                 {
  333.                   while (c1 != end_char)
  334.                     {
  335.                       c1 = *t1++;
  336.                       if (! isspace (c1))
  337.                         {
  338.                           --t1;
  339.                           c1 = ' ';
  340.                           break;
  341.                         }
  342.                     }
  343.                 }
  344.  
  345.               /* Likewise for line 2.  */
  346.               if (isspace (c2))
  347.                 {
  348.                   while (c2 != end_char)
  349.                     {
  350.                       c2 = *t2++;
  351.                       if (! isspace (c2))
  352.                         {
  353.                           --t2;
  354.                           c2 = ' ';
  355.                           break;
  356.                         }
  357.                     }
  358.                 }
  359.             }
  360.  
  361.           /* Upcase all letters if -i is specified.  */
  362.  
  363.           if (ignore_case_flag)
  364.             {
  365.               if (islower (c1))
  366.                 c1 = toupper (c1);
  367.               if (islower (c2))
  368.                 c2 = toupper (c2);
  369.             }
  370.  
  371.           if (c1 != c2)
  372.             break;
  373.           if (c1 == end_char)
  374.             return 0;
  375.         }
  376.     }
  377.  
  378.   return (1);
  379. }
  380.  
  381. /* Find the consecutive changes at the start of the script START.
  382.    Return the last link before the first gap.  */
  383.  
  384. struct change *
  385. find_change (start)
  386.      struct change *start;
  387. {
  388.   return start;
  389. }
  390.  
  391. struct change *
  392. find_reverse_change (start)
  393.      struct change *start;
  394. {
  395.   return start;
  396. }
  397.  
  398. /* Divide SCRIPT into pieces by calling HUNKFUN and
  399.    print each piece with PRINTFUN.
  400.    Both functions take one arg, an edit script.
  401.  
  402.    HUNKFUN is called with the tail of the script
  403.    and returns the last link that belongs together with the start
  404.    of the tail.
  405.  
  406.    PRINTFUN takes a subscript which belongs together (with a null
  407.    link at the end) and prints it.  */
  408.  
  409. void
  410. print_script (script, hunkfun, printfun)
  411.      struct change *script;
  412.      struct change * (*hunkfun) PARAMS((struct change *));
  413.      void (*printfun) PARAMS((struct change *));
  414. {
  415.   struct change *next = script;
  416.  
  417.   while (next)
  418.     {
  419.       struct change *this, *end;
  420.  
  421.       /* Find a set of changes that belong together.  */
  422.       this = next;
  423.       end = (*hunkfun) (next);
  424.  
  425.       /* Disconnect them from the rest of the changes,
  426.          making them a hunk, and remember the rest for next iteration.  */
  427.       next = end->link;
  428.       end->link = 0;
  429. #ifdef DEBUG
  430.       debug_script (this);
  431. #endif
  432.  
  433.       /* Print this hunk.  */
  434.       (*printfun) (this);
  435.  
  436.       /* Reconnect the script so it will all be freed properly.  */
  437.       end->link = next;
  438.     }
  439. }
  440.  
  441. /* Print the text of a single line LINE,
  442.    flagging it with the characters in LINE_FLAG (which say whether
  443.    the line is inserted, deleted, changed, etc.).  */
  444.  
  445. void
  446. print_1_line (line_flag, line)
  447.      char const *line_flag;
  448.      char const * const *line;
  449. {
  450.   char const *text = line[0], *limit = line[1]; /* Help the compiler.  */
  451.   FILE *out = outfile; /* Help the compiler some more.  */
  452.   char const *flag_format = 0;
  453.  
  454.   /* If -T was specified, use a Tab between the line-flag and the text.
  455.      Otherwise use a Space (as Unix diff does).
  456.      Print neither space nor tab if line-flags are empty.  */
  457.  
  458.   if (line_flag && *line_flag)
  459.     {
  460.       flag_format = tab_align_flag ? "%s\t" : "%s ";
  461.       fprintf (out, flag_format, line_flag);
  462.     }
  463.  
  464.   output_1_line (text, limit, flag_format, line_flag);
  465.  
  466.   if ((!line_flag || line_flag[0]) && limit[-1] != '\n'
  467.       && line_end_char == '\n')
  468.     fprintf (out, "\n\\ No newline at end of file\n");
  469. }
  470.  
  471. /* Output a line from TEXT up to LIMIT.  Without -t, output verbatim.
  472.    With -t, expand white space characters to spaces, and if FLAG_FORMAT
  473.    is nonzero, output it with argument LINE_FLAG after every
  474.    internal carriage return, so that tab stops continue to line up.  */
  475.  
  476. void
  477. output_1_line (text, limit, flag_format, line_flag)
  478.      char const *text, *limit, *flag_format, *line_flag;
  479. {
  480.   if (!tab_expand_flag)
  481.     fwrite (text, sizeof (char), limit - text, outfile);
  482.   else
  483.     {
  484.       register FILE *out = outfile;
  485.       register unsigned char c;
  486.       register char const *t = text;
  487.       register unsigned column = 0;
  488.  
  489.       while (t < limit)
  490.         switch ((c = *t++))
  491.           {
  492.           case '\t':
  493.             {
  494.               unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
  495.               column += spaces;
  496.               do
  497.                 putc (' ', out);
  498.               while (--spaces);
  499.             }
  500.             break;
  501.  
  502.           case '\r':
  503.             putc (c, out);
  504.             if (flag_format && t < limit && *t != '\n')
  505.               fprintf (out, flag_format, line_flag);
  506.             column = 0;
  507.             break;
  508.  
  509.           case '\b':
  510.             if (column == 0)
  511.               continue;
  512.             column--;
  513.             putc (c, out);
  514.             break;
  515.  
  516.           default:
  517.             if (isprint (c))
  518.               column++;
  519.             putc (c, out);
  520.             break;
  521.           }
  522.     }
  523. }
  524.  
  525. int
  526. change_letter (inserts, deletes)
  527.      int inserts, deletes;
  528. {
  529.   if (!inserts)
  530.     return 'd';
  531.   else if (!deletes)
  532.     return 'a';
  533.   else
  534.     return 'c';
  535. }
  536.  
  537. /* Translate an internal line number (an index into diff's table of lines)
  538.    into an actual line number in the input file.
  539.    The internal line number is LNUM.  FILE points to the data on the file.
  540.  
  541.    Internal line numbers count from 0 starting after the prefix.
  542.    Actual line numbers count from 1 within the entire file.  */
  543.  
  544. int
  545. translate_line_number (file, lnum)
  546.      struct file_data const *file;
  547.      int lnum;
  548. {
  549.   return lnum + file->prefix_lines + 1;
  550. }
  551.  
  552. void
  553. translate_range (file, a, b, aptr, bptr)
  554.      struct file_data const *file;
  555.      int a, b;
  556.      int *aptr, *bptr;
  557. {
  558.   *aptr = translate_line_number (file, a - 1) + 1;
  559.   *bptr = translate_line_number (file, b + 1) - 1;
  560. }
  561.  
  562. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  563.    If the two numbers are identical, print just one number.
  564.  
  565.    Args A and B are internal line numbers.
  566.    We print the translated (real) line numbers.  */
  567.  
  568. void
  569. print_number_range (sepchar, file, a, b)
  570.      char sepchar;
  571.      struct file_data *file;
  572.      int a, b;
  573. {
  574.   int trans_a, trans_b;
  575.   translate_range (file, a, b, &trans_a, &trans_b);
  576.  
  577.   /* Note: we can have B < A in the case of a range of no lines.
  578.      In this case, we should print the line number before the range,
  579.      which is B.  */
  580.   if (trans_b > trans_a)
  581.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  582.   else
  583.     fprintf (outfile, "%d", trans_b);
  584. }
  585.  
  586. /* Look at a hunk of edit script and report the range of lines in each file
  587.    that it applies to.  HUNK is the start of the hunk, which is a chain
  588.    of `struct change'.  The first and last line numbers of file 0 are stored in
  589.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
  590.    Note that these are internal line numbers that count from 0.
  591.  
  592.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  593.  
  594.    Also set *DELETES nonzero if any lines of file 0 are deleted
  595.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  596.    If only ignorable lines are inserted or deleted, both are
  597.    set to 0.  */
  598.  
  599. void
  600. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  601.      struct change *hunk;
  602.      int *first0, *last0, *first1, *last1;
  603.      int *deletes, *inserts;
  604. {
  605.   int l0, l1, show_from, show_to;
  606.   int i;
  607.   int trivial = ignore_blank_lines_flag || ignore_regexp_list;
  608.   struct change *next;
  609.  
  610.   show_from = show_to = 0;
  611.  
  612.   *first0 = hunk->line0;
  613.   *first1 = hunk->line1;
  614.  
  615.   next = hunk;
  616.   do
  617.     {
  618.       l0 = next->line0 + next->deleted - 1;
  619.       l1 = next->line1 + next->inserted - 1;
  620.       show_from += next->deleted;
  621.       show_to += next->inserted;
  622.  
  623.       for (i = next->line0; i <= l0 && trivial; i++)
  624.         if (!ignore_blank_lines_flag || files[0].linbuf[i][0] != '\n')
  625.           {
  626.             struct regexp_list *r;
  627.             char const *line = files[0].linbuf[i];
  628.             int len = files[0].linbuf[i + 1] - line;
  629.  
  630.             for (r = ignore_regexp_list; r; r = r->next)
  631.               if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  632.                 break;  /* Found a match.  Ignore this line.  */
  633.             /* If we got all the way through the regexp list without
  634.                finding a match, then it's nontrivial.  */
  635.             if (!r)
  636.               trivial = 0;
  637.           }
  638.  
  639.       for (i = next->line1; i <= l1 && trivial; i++)
  640.         if (!ignore_blank_lines_flag || files[1].linbuf[i][0] != '\n')
  641.           {
  642.             struct regexp_list *r;
  643.             char const *line = files[1].linbuf[i];
  644.             int len = files[1].linbuf[i + 1] - line;
  645.  
  646.             for (r = ignore_regexp_list; r; r = r->next)
  647.               if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  648.                 break;  /* Found a match.  Ignore this line.  */
  649.             /* If we got all the way through the regexp list without
  650.                finding a match, then it's nontrivial.  */
  651.             if (!r)
  652.               trivial = 0;
  653.           }
  654.     }
  655.   while ((next = next->link) != 0);
  656.  
  657.   *last0 = l0;
  658.   *last1 = l1;
  659.  
  660.   /* If all inserted or deleted lines are ignorable,
  661.      tell the caller to ignore this hunk.  */
  662.  
  663.   if (trivial)
  664.     show_from = show_to = 0;
  665.  
  666.   *deletes = show_from;
  667.   *inserts = show_to;
  668. }
  669.  
  670. /* malloc a block of memory, with fatal error message if we can't do it. */
  671.  
  672. VOID *
  673. xmalloc (size)
  674.      size_t size;
  675. {
  676.   register VOID *value;
  677.  
  678.   if (size == 0)
  679.     size = 1;
  680.  
  681.   value = (VOID *) malloc (size);
  682.  
  683.   if (!value)
  684.     fatal ("memory exhausted");
  685.   return value;
  686. }
  687.  
  688. /* realloc a block of memory, with fatal error message if we can't do it. */
  689.  
  690. VOID *
  691. xrealloc (old, size)
  692.      VOID *old;
  693.      size_t size;
  694. {
  695.   register VOID *value;
  696.  
  697.   if (size == 0)
  698.     size = 1;
  699.  
  700.   value = (VOID *) realloc (old, size);
  701.  
  702.   if (!value)
  703.     fatal ("memory exhausted");
  704.   return value;
  705. }
  706.  
  707. /* Concatenate three strings, returning a newly malloc'd string.  */
  708.  
  709. char *
  710. concat (s1, s2, s3)
  711.      char const *s1, *s2, *s3;
  712. {
  713.   size_t len = strlen (s1) + strlen (s2) + strlen (s3);
  714.   char *new = xmalloc (len + 1);
  715.   sprintf (new, "%s%s%s", s1, s2, s3);
  716.   return new;
  717. }
  718.  
  719. /* Yield the newly malloc'd pathname
  720.    of the file in DIR whose filename is FILE.  */
  721.  
  722. char *
  723. dir_file_pathname (dir, file)
  724.      char const *dir, *file;
  725. {
  726. #ifdef _AMIGA
  727.   int l = strlen(dir);
  728.  
  729.   return concat (dir, "/" + (!l || dir[l - 1] == '/' || dir[l - 1] == ':'
  730.                             ), file);
  731. #else
  732.   return concat (dir, "/" + (*dir && dir[strlen (dir) - 1] == '/'), file);
  733. #endif
  734. }
  735.  
  736. void
  737. debug_script (sp)
  738.      struct change *sp;
  739. {
  740.   fflush (stdout);
  741.   for (; sp; sp = sp->link)
  742.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  743.              sp->line0, sp->line1, sp->deleted, sp->inserted);
  744.   fflush (stderr);
  745. }
  746.  
  747. #if !HAVE_MEMCHR
  748. char *
  749. memchr (s, c, n)
  750.      char const *s;
  751.      int c;
  752.      size_t n;
  753. {
  754.   unsigned char const *p = (unsigned char const *) s, *lim = p + n;
  755.   for (;  p < lim;  p++)
  756.     if (*p == c)
  757.       return (char *) p;
  758.   return 0;
  759. }
  760. #endif
  761.